<?php
class Event_WidgetController extends Clu_Controller_Action_Widget
{
	public function init()
	{
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		//require_once (APP_PATH.'/modules/place/Model/City.php');
		//require_once (APP_PATH.'/modules/category/Model/Category.php');
		$this->mArticle = new article_Model_Service();
		$this->m = new event_Model_Service();
		$this->mCity = new place_Model_City();
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('event');
		$this->m = new event_Model_Service();
		parent::init();
	}
	
	public function featuredEventsHomeAction()
	{
		//require_once (APP_PATH . "/modules/admin/Model/Weight.php");
    	$mWeight = new admin_Model_Weight();
    	//now get all the content_id from this $widgetPositionId;
    	$data['widget_position_id'] = $this->widget['id'] ;
    	$currentItems = $mWeight->searchCurrentItemsByWidgetPositionId($data);
    	//die_a($currentItems);
    	$contents = array();
		foreach ($currentItems as $item)
		{
			$contents[] = $this->m->getData($item['content_id'], array('get_article' => 1));
		}
		$this->viewParams['eventList'] = $contents;
    	/*
		$data = array('status' => 'featured','item_per_page' => 4);
		$paginatorEvent = $this->getListEvent($data);
       	$this->viewParams['paginator'] = $paginatorEvent;
		$this->viewParams['eventList'] =  $paginatorEvent->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->viewParams['eventList'] as $e)
		{
			$articleList[$e['id']] = $mArticle->getArticleData($e['article_id']);
		}
		$this->viewParams['aList'] = $articleList;
		*/
	}
	
	public function	featuredEventsAction()
	{
		$data = array('status' => 'featured','item_per_page' => 4);
		$paginatorEvent = $this->getListEvent($data);
       	$this->viewParams['paginator'] = $paginatorEvent;
		$this->viewParams['eventList'] =  $paginatorEvent->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		//die_a($this->viewParams['eventList']);
		foreach ($this->viewParams['eventList'] as $e)
		{
			$articleList[$e['id']] = $mArticle->getArticleData($e['article_id']);
		}
		$this->viewParams['aList'] = $articleList;
		
		// This is a dirty hacks to include both place galleries in to this widget
		$sess = new Zend_Session_Namespace(CLU_NS);
		if ($sess->masterRequest == "hvn/master/home")
		{
			//get the place galleries here
			
		}
		
	}
	public function upcomingEventsAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;
		$select = $db->select();
		// Add a FROM clause
        $select->from( array('e' => 'events'));
        $select->order(array('e.start_date DESC', 'id'));
   if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
        $paginator = $this->generatePaginator($select, $data['page'], $data['item_per_page'], 7, 'Jumping');
	 	    if(isset($paginator))
       {	
           		$searchUrl = encode_search_url($data, "event");
			$this->viewParams['searchUrl'] = 
			"http://" . SITE_URL . $this->widgetService->getView()->hvnUrl(array('action' => 'search', 'module' => "event"),'nocontroller') . "?$searchUrl";
           
	       	$this->viewParams['paginator'] = $paginator;
			$this->viewParams['eList'] = $paginator->getCurrentItems();
       }	
	foreach($this->viewParams['eList']  as $key=>$value){
            $listArticle[$value['id']]  =   $this->mArticle->getArticleData($value['article_id']);
            $listArticle[$value['id']]['article_body']  		= 	substring_n_words(strip_tags($listArticle[$value['id']]['article_body']  ,'<div <span ') ,50);   
    	}
		if(!empty($listArticle)){
			$this->viewParams['listArticle']   = $listArticle;
		}else{
			$this->viewParams['listArticle'] = array() ;
		}
	 	 $listCity = array();//TODO 
		$listCity = $this->mCity->findForSelect();
		$this->viewParams['listCity'] = $listCity;  
		$this->viewParams['listCategory'] = $this->mCategory->getRootType('Root_Event');
	 	
	}
	
	public function newEventsAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;
		$sql = "SELECT *  FROM events  ORDER BY start_date DESC limit 1";
		$this->viewParams['eList'] = $this->db->fetchAll($sql);
		$listArticle[$this->viewParams['eList'][0]['id']]  =   $this->mArticle->getArticleData($this->viewParams['eList'][0]['article_id']);
		$listArticle[$this->viewParams['eList'][0]['id']]['article_body']  		= 		substring_n_words(strip_tags($listArticle[$this->viewParams['eList'][0]['id']]['article_body']  ,'<p ,<div <font ') ,100);
		if(!empty($listArticle)){
			$this->viewParams['listArticle']   = $listArticle;
		}else{
			$this->viewParams['listArticle'] = array() ;
		}
	}
	public function getListEvent($data)
	{
		if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10 ;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
	   	$select = $this->m->search($data);
	   	$paginator = $this->generatePaginator($select, $data['page'], $data['item_per_page'], 5, 'Jumping');
	   	$sess = new Zend_Session_Namespace("paginatorEvent");
	   	$sess->event_list = array("event_params" => $data);
        $data = $sess->event_list;
				
      	//cache in $_SESSION
        if(isset($data['page']))
        {
   			$select = $this->m->search($data);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['item_per_page'],5, 'Jumping');
        }
        return $paginator;
	}
	
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
    
    public function HvnImageshow ($data)
    {
    	$userId = $data['userId'];
    	$contentType = $data['contentType'];
    	$id = $data['contentId'];
    	$size = $data['size'];
    	$dir = APP_PATH."/public/ufiles/$userId/images/$size/$contentType/";
    	if(file_exists($dir))
		{
		    $files = scandir($dir);
		    $displayFiles = array();
		    foreach ($files as $f)
		    {
		    	if (strpos ($f, $id . "_") === 0)
		    	{
		    		$ctime = filectime($dir . $f); 
 		    		$displayFiles[$f] = $ctime;
		    	}
		    }
	    }
    	if (isset($displayFiles) && count($displayFiles) > 0)
    	{
    		asort($displayFiles);
    		$ret = "";
		    foreach ($displayFiles as $thumb => $ctime)
		    {
    			$ret .= "<img src='/public/ufiles/$userId/images/$size/$contentType/$thumb' />";
    			break;
		    }
    	}
    	else
    		$ret = "<img src='/public/themes/prime/images/common/75x75_no_image.gif' />";
	    return $ret;
    } 
    
    public function editTabMenuAction()
    {
//      $this->viewParams['id'] = $this->getParam('id');
//      $this->viewParams['currentTab'] = $this->getParam('currentTab');
        $this->viewParams['id'] = $_REQUEST['id'];
        //$this->viewParams['currentTab'] = "thumbnail";
    }
    
    public function adsOfEventsAction()
    {
    	
    }
    
    public function postedEventAction()
    {
    	$id = $this->getParam('content_id');
    	$this->viewParams['id'] = $id;
    	$dataEvent = $this->m->search(array('status' => 'published'));
        $this->viewParams['old_news'] = $this->db->fetchAll($dataEvent);
        $this->m->updateDbAfterView($id);
    }
}
?>
